home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacGofer 0.22d / MacGofer Sources / mac_humayan_utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-08  |  6.9 KB  |  256 lines  |  [TEXT/MPS ]

  1. #include "mac.h"
  2. #include <AppleEvents.h>
  3.  
  4. #pragma segment Humayan
  5.  
  6. void HLockHigh( Handle h )
  7. /* Pre:  none */
  8. /* Post: h has been moved to the top of the heap if possible and then locked */
  9. {
  10.     MoveHHi(h);
  11.     HLock(h);
  12. }
  13.  
  14. void SafeHLock( Handle h, Boolean *saveLock )
  15. /* Pre:  none */
  16. /* Post: h is locked if it wasn't; *saveLock is set to the status of the lock on entry */
  17. {
  18.     #define lockBit    (1 << 7)
  19.  
  20.     *saveLock = (HGetState(h) & lockBit) != 0;
  21.     if ( !(*saveLock) )
  22.         HLockHigh(h);
  23. }
  24.  
  25. void SafeHUnlock( Handle h, Boolean saveLock )
  26. /* Pre:  saveLock is the status of the handle's lock at the last call to SafeHLock */
  27. /* Post: h is unlocked if saveLock is false */
  28. {
  29.     if ( !saveLock )
  30.         HUnlock(h);
  31. }
  32.  
  33. OSErr GotRequiredAEParams(theAppleEvent)
  34. AppleEvent *theAppleEvent;
  35. /* Confirms that all required parameters have been extracted from an Apple event */
  36. {
  37.   DescType returnedType;
  38.   Size actualSize;
  39.   OSErr resultCode;
  40.  
  41.   resultCode = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, &returnedType, NIL, 0, &actualSize);
  42.   if (resultCode == errAEDescNotFound)
  43.     return noErr;
  44.   else
  45.     return errAEEventNotHandled;
  46. }
  47.  
  48. pascal OSErr HandleOAPP(theAppleEvent, reply, handlerRefcon)
  49. AppleEvent *theAppleEvent, *reply;
  50. long handlerRefcon;
  51. /* Pre:  none */
  52. /* Post: Open Application Apple event has been handled */
  53. {
  54.   OSErr resultCode;
  55.  
  56.   resultCode = GotRequiredAEParams (theAppleEvent);
  57.   if (resultCode != noErr)
  58.     return resultCode;
  59.  
  60.   InitApp();
  61. }
  62.  
  63. pascal OSErr HandleODOC(theAppleEvent, reply, handlerRefcon)
  64. AppleEvent *theAppleEvent, *reply;
  65. long handlerRefcon;
  66. /* Pre:  none */
  67. /* Post: Open Documents Apple event has been handled */
  68. {
  69.   OSErr      resultCode;
  70.   AEKeyword  keywd;
  71.   AEDescList docList;
  72.   DescType   returnedType;
  73.   FSSpec     fsspec;
  74.   int        items;
  75.   int        i;
  76.   Size       actualSize;
  77.   FInfo    finderinfo;
  78.    
  79.   InitApp();
  80.  
  81.   if((resultCode = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList)) == noErr)
  82.     {
  83.       if((resultCode = AECountItems (&docList, &items)) == noErr)
  84.         {
  85.            for(i=1; i <= items; ++i)
  86.              if(AEGetNthPtr(&docList,i,typeFSS,&keywd,&returnedType,(Ptr)&fsspec,sizeof(fsspec),&actualSize) == noErr)
  87.                {
  88.                  HGetFInfo(fsspec.vRefNum,fsspec.parID,fsspec.name,&finderinfo);
  89.                  OpenDoc(fsspec,finderinfo.fdType);
  90.                }
  91.              else
  92.                SysBeep(1);
  93.  
  94.         }
  95.     }
  96.  
  97.   if (resultCode != noErr)
  98.     return resultCode;
  99. }
  100.  
  101. pascal OSErr HandlePDOC(theAppleEvent, reply, handlerRefcon)
  102. AppleEvent *theAppleEvent, *reply;
  103. long handlerRefcon;
  104. /* Pre:  none */
  105. /* Post: Print Documents Apple event has been handled */
  106. {
  107.   OSErr      resultCode;
  108.   AEKeyword  keywd;
  109.   AEDescList docList;
  110.   DescType   returnedType;
  111.   FSSpec     fsspec;
  112.   int        items;
  113.   int        i;
  114.   Size       actualSize;
  115.   
  116.   if((resultCode = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList)) == noErr)
  117.     {
  118.       if((resultCode = AECountItems (&docList, &items)) == noErr)
  119.         {
  120.            for(i=1; i <= items; ++i)
  121.              if(AEGetNthPtr(&docList,i,typeFSS,&keywd,&returnedType,(Ptr)&fsspec,sizeof(fsspec),&actualSize) == noErr)
  122.                PrintDoc(fsspec);
  123.              else
  124.                SysBeep(1);
  125.         }
  126.     }
  127.  
  128.   if (resultCode != noErr)
  129.     return resultCode;
  130. }
  131.  
  132. pascal OSErr HandleQUIT(theAppleEvent, reply, handlerRefcon)
  133. AppleEvent *theAppleEvent, *reply;
  134. long handlerRefcon;
  135. /* Pre:  none */
  136. /* Post: Quit Apple event has been handled */
  137. {
  138.   OSErr      resultCode;
  139.   DescType   saveOpt = kAEAskUser;
  140.   DescType   returnedType;
  141.   Size       actualSize;
  142.   extern OSErr QuitApp();
  143.   
  144.   if((resultCode = AEGetParamPtr(theAppleEvent,keyAESaveOptions,typeEnumerated,
  145.       &returnedType,(Ptr) &saveOpt,sizeof(saveOpt),&actualSize)) == noErr)
  146.     resultCode = GotRequiredAEParams (theAppleEvent);
  147.  
  148.   return( QuitApp(saveOpt) );
  149. }
  150.  
  151. OSErr InstallAEHandlers(void)
  152. /* Pre:  The Apple events Manager is present */
  153. /* Post: If return value is noErr, Apple event handlers have been installed */
  154. /*         If return value is not noErr, a fatal error occurred during installation */
  155. {
  156.   OSErr resultCode;
  157.  
  158.   resultCode = AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, 
  159.                                        (EventHandlerProcPtr)&HandleOAPP, 0, false);
  160.   if (resultCode != noErr)
  161.     return resultCode;
  162.  
  163.   resultCode = AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, 
  164.                                        (EventHandlerProcPtr)&HandleODOC, 0, false);
  165.   if (resultCode != noErr)
  166.     return resultCode;
  167.  
  168.   resultCode = AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, 
  169.                                        (EventHandlerProcPtr)&HandlePDOC, 0, false);
  170.   if (resultCode != noErr)
  171.     return resultCode;
  172.  
  173.   resultCode = AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, 
  174.                                        (EventHandlerProcPtr)&HandleQUIT, 0, false);
  175.   return resultCode;
  176. }
  177.  
  178.  
  179. /* C Versions of necessary HFS routines            */
  180. /* Should probably be moved into a separate module    */
  181.  
  182. OSErr hcreate(char *fileName,short vRefNum,long dirID,OSType creator,OSType fileType)
  183. {
  184.   OSErr resultCode = HCreate(vRefNum,dirID,c2pstr(fileName),creator,fileType);
  185.   fileName = p2cstr((StringPtr)fileName);
  186.   return resultCode;
  187. }
  188.  
  189. OSErr hopen(char *fileName,short vRefNum,long dirID,char permission,short *refNum)
  190. {
  191.   OSErr resultCode = HOpen(vRefNum,dirID,c2pstr(fileName),permission,refNum);
  192.   fileName = p2cstr((StringPtr)fileName);
  193.   return resultCode;
  194. }
  195.  
  196. OSErr hrename(char *oldName,short vRefNum,long dirID,char *newName)
  197. {
  198.   OSErr resultCode = HRename(vRefNum,dirID,c2pstr(oldName),c2pstr(newName));
  199.   oldName = p2cstr((StringPtr)oldName);
  200.   newName = p2cstr((StringPtr)newName);
  201.   return resultCode;
  202. }
  203.  
  204. OSErr hdelete(char *fileName,short vRefNum,long dirID)
  205. {
  206.   OSErr resultCode = HDelete(vRefNum,dirID,c2pstr(fileName));
  207.   fileName = p2cstr((StringPtr)fileName);
  208.   return resultCode;
  209. }
  210.  
  211. OSErr hgetfinfo(char *fileName,short vRefNum,long dirID,FInfo *fndrInfo)
  212. {
  213.   OSErr resultCode = HGetFInfo(vRefNum,dirID,c2pstr(fileName),fndrInfo);
  214.   fileName = p2cstr((StringPtr)fileName);
  215.   return resultCode;
  216. }
  217.  
  218. OSErr hsetfinfo(char *fileName,short vRefNum,long dirID,FInfo *fndrInfo)
  219. {
  220.   OSErr resultCode = HSetFInfo(vRefNum,dirID,c2pstr(fileName),fndrInfo);
  221.   fileName = p2cstr((StringPtr)fileName);
  222.   return resultCode;
  223. }
  224.  
  225. OSErr getwdinfo(short theWDRefNum,short *vRefNum,long *dirID)
  226. {
  227.   long dummyProcID;
  228.  
  229.   return GetWDInfo(theWDRefNum, vRefNum, dirID, &dummyProcID);
  230. }
  231.  
  232. OSErr fsmakefsspec(char *fileName,short vRefNum,long dirID,FSSpec *spec)
  233. {
  234.   OSErr resultCode = FSMakeFSSpec(vRefNum, dirID, c2pstr(fileName), spec);
  235.   fileName = p2cstr((StringPtr)fileName);
  236.   return resultCode;
  237. }
  238.  
  239. void pstrcopy(unsigned char *src, unsigned char *dst)
  240. {
  241.   short count = (short)(*src) + 1;
  242.   while (count--)
  243.     *dst++ = *src++;
  244. }
  245.  
  246. void SFReplyToFSSpec(SFReply theReply,FSSpec *theSpec)
  247. {
  248.   if (getwdinfo(theReply.vRefNum, &theSpec->vRefNum, &theSpec->parID) == noErr)
  249.     pstrcopy(theReply.fName, theSpec->name);
  250.   else {
  251.     theSpec->vRefNum = 0;
  252.     theSpec->parID = 0;
  253.     theSpec->name[0] = 0;
  254.   }
  255. }
  256.